(function () {
	var serverData = {};
    var serverTable;
    jQuery('.search_btn').on('click',function(){
    	clearQueryServerData();
    	getServerDate();
        serverTable.draw();
    });
    
    var getServerDate=function(){
    	if(jQuery("#orderArea").val()){
    		serverData.orderArea=jQuery("#orderArea").val().toString();
    		orderArea = jQuery("#orderArea").val().toString();
    	}
    	if(jQuery("#industryInfo").val()){
    		serverData.detailType=jQuery("#industryInfo").val().toString();
    		detailType = jQuery("#industryInfo").val().toString();
    	}
    	if(jQuery("#creditLevel").val()){
    		serverData.industryInfo=jQuery("#creditLevel").val().toString();
    		industryInfo = jQuery("#creditLevel").val().toString();
    	}
    	if(jQuery("#startDate").val()){
    		serverData.createTimeStartDate=jQuery("#startDate").val().toString();
    		createTimeStartDate = jQuery("#startDate").val().toString();
    	}
    	if(jQuery("#endDate").val()){
    		serverData.createTimeEndDate=jQuery("#endDate").val();
    		createTimeEndDate = jQuery("#endDate").val().toString();
    	}
    	if(jQuery("#chanceCount").val()){
    		serverData.nUnitPrice=jQuery("#chanceCount").val().toString();
    		nUnitPrice = jQuery("#chanceCount").val().toString();
    	}
    	if(jQuery("#supplierCode").val()){
    		serverData.supplierCode=jQuery("#supplierCode").val().toString();
    		supplierCode = jQuery("#supplierCode").val().toString();
    	}
    	if(jQuery("#orderUnit").val()){
    		serverData.orderUnit=jQuery("#orderUnit").val();
    		orderUnit = jQuery("#orderUnit").val();
    	}
    }
    
    var clearQueryServerData=function(){
    	if(serverData.orderArea) {
    		serverData.orderArea=null;
    		orderArea=null;
    	}
    	if(serverData.detailType) {
    		serverData.detailType=null;
    		detailType=null;
    	}
    	if(serverData.industryInfo) {
    		serverData.industryInfo=null;
    		industryInfo=null;
    	}
    	if(serverData.createTimeStartDate) {
    		serverData.createTimeStartDate=null;
    		createTimeStartDate=null;
    	}
    	if(serverData.createTimeEndDate) {
    		serverData.createTimeEndDate=null;
    		createTimeEndDate=null;
    	}
    	if(serverData.nUnitPrice) {
    		serverData.nUnitPrice=null;
    		nUnitPrice=null;
    	}
    	if(serverData.supplierCode) {
    		serverData.supplierCode=null;
    		supplierCode=null;
    	}
    	if(serverData.orderUnit) {
    		serverData.orderUnit=null;
    		orderUnit=null;
    	}
    }
    //初始化查询条件
    function initQueryParam(){
    	if($('#endDate').val()== "") 
    	$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+new Date().getDate());
    	if($('#startDate').val() == "")
    	$('#startDate').val(new Date().getFullYear()+"-01-01");
    }
    jQuery(document).ready(function () {
    	$("#orderArea").multiselect({
		      header: true,
		      height: 175,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
		
		$("#generateState").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
		
		$("#creditLevel").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
		
		$("#industryInfo").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
		$("#zican").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
		$("#chanceCount").multiselect({
		      header: true,
		      height: 250,
		      minWidth: 150,
		      classes: '',
		      checkAllText: '选中全部',
		      uncheckAllText: '取消全选',
		      noneSelectedText: '请勾选',
		      selectedText: '# 选中',
		      selectedList: 5,
		      show: null,
		      hide: null,
		      autoOpen: false,
		      multiple: true,
		      position: {},
		      appendTo: "body",
		      menuWidth:null
		    });
		getServerDate();
    	serverTable = jQuery('#dataTable').DataTable({
    		"serverSide": true,
            "ordering": false,
    		"ajax": {
    			"dataType": "json",
    			"type": "get",
    			"cache": false,
    			"url":window.rootPath+ '/ui/productTotal/getProductTotalPage',
    			"data": function (d) {
    				return $.extend({}, d, serverData);
    			}
    		},
    	    "columns" : [
				{
					data : null,
                    defaultContent : ""// 排序   1
				},
	            {
					data : 'mainType'
				},
				
	            {
					data : 'detailType'
				},
				{
					data : 'nNumber'
				},{
					data : 'industryInfo'
				},
	            {
					data : 'contractType'
				},{
					data: 'supplierCode'
				},
	            {
					data : 'signedDate',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
				},
	            {
					data : 'orderUnit'
				},
	            {
					data : 'nUnitPrice'
				},
	            {
					data : 'orderArea'
				},{
					data : 'createTime',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                    }
				}
				],
			    "drawCallback": function (oSettings) {
			        var k = 0;
			        var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
			        for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
			            $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
			            k++;
			        }
			    }	
	    	});
    	serverTable.on('dblclick','tr',function(event) {
        	event = event|| window.event;
        	var target = event.target|| event.scrElement;
        	var data = serverTable.row(this).data();
        	var id=data.sContractID;
        	var taskId="0";
		    var type=data.contractType;
		    var url=window.rootPath+'/ui/contract/audit-view/'+ taskId + '/' + id +'/'+ type;
		    window.location.href = url;
        	return;
        });
		
		
    });
})();